package com.thx.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具.
 */
public class DateUtil {
  static Logger log = LoggerFactory.getLogger(DateUtil.class);

  /**
   * 获取今年的第几周.
   */
  public static String getWeekOfYear() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    return String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
  }

  /**
   * 获取年份.
   */
  public static String getYear() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    return String.valueOf(calendar.get(Calendar.YEAR));

  }

  /**
   * 获取月份, 从 1 开始计算.
   */
  public static String getMonth() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
    if (month.length() == 1) {
      month = "0" + month;
    }
    return month;
  }

  /**
   * 获取指定日期的月份又多少天.
   *
   * @param date 日期
   * @return 月份天数
   */
  public static String getMonth(Date date) {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = date;
    calendar.setTime(trialTime);
    int year = calendar.get(Calendar.YEAR);

    int month = calendar.get(Calendar.MONTH) + 1;

    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
        || month == 12) {
      return "31";
    }
    if (month == 4 || month == 6 || month == 9 || month == 11) {
      return "30";
    }
    if (month == 2) {
      if (year % 4 == 0 || year % 400 == 0) {
        return "29";
      } else {
        return "28";
      }
    }
    return "0";
  }

  /**
   * 取得当天的日期，因为Day在java中习惯理解为星期几，所以为了避免混淆 不推荐用此方法，推荐用getDate()方法.
   *
   * @return 当天的日期，如果当天是五号，则返回05，不足两位前面补0
   * @deprecated {@see getDate()}
   */
  public static String getDay() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String day = String.valueOf(calendar.get(Calendar.DATE));
    if (day.length() == 1) {
      day = "0" + day;
    }
    return day;
  }

  /**
   * 取得当天的日期.
   *
   * @return 当天的日期，如果当天是五号，则返回05，不足两位前面补0
   */
  public static String getDate() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String day = String.valueOf(calendar.get(Calendar.DATE));
    if (day.length() == 1) {
      day = "0" + day;
    }
    return day;
  }

  /**
   * 取得当天的星期.
   *
   * @return 当天的星期，如果星期五，则返回6，返回的字符串长度为1
   */
  public static String getDayOfWeek() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String day = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
    return day;
  }

  /**
   * 取当前小时.
   *
   * @return 24 小时格式,不足 2 位前面补 0
   */
  public static String getHour() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
    if (hour.length() == 1) {
      hour = "0" + hour;
    }
    return hour;

  }

  /**
   * 获取当前分钟.
   *
   * @return 当前分钟, 不足 2 位前面补 0
   */
  public static String getMinute() {
    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String minute = String.valueOf(calendar.get(Calendar.MINUTE));
    if (minute.length() == 1) {
      minute = "0" + minute;
    }
    return minute;

  }

  /**
   * 取得前一天日期.
   *
   * @return 前一天日期，如果前一天是五号，则返回05，不足两位前面补0
   */
  public static String getBeforeDay() {

    Calendar calendar = new GregorianCalendar();
    Date trialTime = new Date();
    calendar.setTime(trialTime);
    String day = String.valueOf(calendar.get(Calendar.DATE) - 1);
    if (day.length() == 1) {
      day = "0" + day;
    }
    return day;
  }

  /**
   * 今天星期几.
   *
   * @author liuh
   */
  public int getWeekByDate(String dateParam) throws ParseException {
    int weekNum = 0;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    if (StringUtils.isNotBlank(dateParam)) {
      date = simpleDateFormat.parse(dateParam);
    } else {
      date = new Date();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    weekNum = calendar.get(Calendar.DAY_OF_WEEK);
    weekNum = weekNum - 1;
    return weekNum;
  }

  /**
   * 设置天和小时.
   *
   * @author liuh
   */
  public static Date addDay(int day, int hours, int minutes) throws ParseException {
    Date date = DateUtils.addDays(new Date(), day);
    Date date1 = DateUtils.setMinutes(DateUtils.setHours(date, hours), minutes);
    return date1;
  }

  /**
   * 月的第一天和最后一天.
   *
   * @author liuh
   */
  public static String getDayByMonth(Date date, String flag) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    calendar.setTime(date);
    if ("stat".equals(flag)) {
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    } else {
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    }
    return simpleDateFormat.format(calendar.getTime());
  }

  /**
   * 根据指定的格式将一个字符串解析为日期.
   *
   * @param date 要解析的日期字符串
   * @param formatStr 格式字符串
   * @return 解析到的日期对象
   */
  public static Date parse(String date, String formatStr) throws ParseException {
    DateFormat df = new SimpleDateFormat(formatStr);
    return df.parse(date);
  }

  /**
   * 根据指定的格式将日期格式化为字符串.
   *
   * @param date 要转换的日期
   * @param formatStr 格式字符串
   * @return 格式化后的字符串
   */
  public static String format(Date date, String formatStr) {
    DateFormat df = new SimpleDateFormat(formatStr);
    return df.format(date);
  }

}
